import 'package:flutter/material.dart';
import 'package:historyToday/config/api.dart';

import 'brower.dart';

class DetailPage extends StatefulWidget {
  DetailPage({Key key, this.mounth, this.day, this.index}) : super(key: key);

  String mounth = '';
  String day = '';
  int index = 0;
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  Map detailData = {'title': null};

  @override
  void initState() {
    getList(widget.mounth, widget.day).then((res) {
      setState(() {
        detailData = res['body'][widget.index];
      });
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return detailData['title'] == null
        ? Container(
            color: Colors.white,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                CircularProgressIndicator(),
                SizedBox(height: 20),
                Text(
                  "正在加载",
                  style: TextStyle(color: Colors.redAccent[700], fontSize: 14),
                )
              ],
            ),
          )
        : Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.redAccent[700],
              title: Text(detailData['title']),
            ),
            body: SingleChildScrollView(
              child: Container(
                padding: EdgeInsets.all(15),
                child: Column(
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.all(Radius.circular(5)),
                      child: FadeInImage.assetNetwork(
                        placeholder: 'assets/images/placeholder.jpeg',
                        fadeInDuration: const Duration(milliseconds: 500),
                        image: detailData['imgUrl'],
                      ),
                    ),
                    SizedBox(
                      height: 20,
                    ),
                    Text(detailData['sub']),
                    RaisedButton(
                      child: Text('查看更多'),
                      onPressed: () {
                        Navigator.of(context)
                            .push(new MaterialPageRoute(builder: (_) {
                          return new Browser(
                            url: detailData['webUrl'],
                            title: "",
                          );
                        }));
                      },
                      textColor: Colors.white,
                      color: Colors.redAccent[700],
                    ),
                  ],
                ),
              ),
            ));
  }
}
